package com.thssh.ui.lsn14.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.thssh.ui.lsn14.ext.IAdapter;

/**
 * 仿照ListView实现一个简单的ListView
 *
 * step1:onMeasure中侧车父控件宽高
 * step2:onLayout中填充一页的子控件
 * step3:循环子控件
 *
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/28
 * email: zhangyugehu@163.com
 */

public class SimpleListView extends ViewGroup {

    /**
     * 1. 得到viewtype
     * 2. 从回收池中获取view
     * 3. 从adapter中获取view
     */

    private int mWidth;
    private int mHeight;

    private IAdapter mAdapter;
    private RecycleBin mRecycleBin;

    private int mLastVisiblePosition;
    private int mFirstVisiblePosition;


    public SimpleListView(Context context) {
        this(context, null);
    }

    public SimpleListView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SimpleListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setAdapter(IAdapter adapter) {
        this.mAdapter = adapter;
        mRecycleBin = new RecycleBin(adapter.getTypeCount());
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthSuggest = MeasureSpec.getSize(widthMeasureSpec);
        int heightSuggest = MeasureSpec.getSize(heightMeasureSpec);
        if(MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY){
            mWidth = widthSuggest;
        }else{
            mWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        }
        if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY){
            mHeight = heightSuggest;
        }else{
            mHeight = getDefaultSize(getSuggestedMinimumWidth(), heightMeasureSpec);
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    private int dy;
    private int startY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean handled = false;
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                handled = true;
                startY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                handled = true;
                dy = (int) (event.getY() - startY);
                requestLayout();
                break;
            case MotionEvent.ACTION_UP:
                handled = false;
                break;
        }
        return handled;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.d("SimpleListView", "reset--------------------------: ");
        if(mAdapter == null){
            return;
        }
        removeAllViews();
        int lastBottom = dy;

        for(int i = 0; i < mAdapter.getCount() && lastBottom < mHeight; i++){
            View child = makeAndAddView(i, lastBottom);
            int left = 0;
            int top = lastBottom;
            int right = left + child.getMeasuredWidth();
            int bottom = top + child.getMeasuredHeight();
            Log.d("SimpleListView", "pos: " + i + " top: " + top + " bottom: " + bottom);
            child.layout(left, top, right, bottom);
            lastBottom = bottom;
        }
    }

    private View makeAndAddView(int position, int lastBottom) {
        View recyclerView = mRecycleBin.getRecyclerView(mAdapter.getItemViewType(position));
        View child = mAdapter.getView(position, recyclerView, this);
//        if(recyclerView == null) {
            child.measure(MeasureSpec.makeMeasureSpec(1000, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY));
//            mRecycleBin.addRecyclerView(child ,mAdapter.getItemViewType(position));
//        }
        if(lastBottom < 0) { return child; }
        addView(child);
        return child;
    }

    class RecycleBin{
        private View[] mRecyclerViews = new View[0];

        public RecycleBin(int typeCount){
            mRecyclerViews = new View[typeCount];
        }

        public void addRecyclerView(View view, int viewType){
            mRecyclerViews[viewType] = view;
        }

        public View getRecyclerView(int viewType){
            return mRecyclerViews[viewType];
        }
    }

}
